package com.kgc.scd.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kgc.scd.bean.Address;
import com.kgc.scd.contant.CommonConstant;
import com.kgc.scd.service.AddressService;
import com.kgc.scd.service.UserService;
import com.kgc.scd.util.MyUtils;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * @author: WangKuoKuo
 * @create: 2022-06-10 08:58
 * @description : 地址簿管理
 **/

@Slf4j
@RestController
// @RequestMapping("/address")
@Api(tags = "地址簿管理")
public class AddressController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private UserService userService;

    /**
     * 新增地址
     *
     * @return
     */
    @PostMapping("/addAddress")
    @ApiOperation("添加地址")

    public RequestResult<Address> addAddress(@RequestBody Address address) {


        if (ObjectUtils.isEmpty(address.getAddrUserName())
                || ObjectUtils.isEmpty(address.getAddrDetail())
                || ObjectUtils.isEmpty(address.getAddrDistrict())
               ) {

            log.info("新增地址信息失败:{}", address);
            return ResultBuildUtil.fail("501", "地址填写不完整保存失败！");
        }

        address.setAddrId(generateAddrID());

        if (addressService.insertAddr(address)) {
            log.info("新增地址信息:{}", address);
            return ResultBuildUtil.success(address);
        }

        log.info("新增地址添加失败！");
        return ResultBuildUtil.fail("502", "新增地址添加失败！");
    }

    public String generateAddrID() {
        // 生成地址ID的组成规则：系统标识+时间字符串+随机序列号
        return "A"
                + LocalDateTime.now().format(DateTimeFormatter.ofPattern(CommonConstant.TIME_PATTERN_MINUTE))
                + MyUtils.generateNonceStrUseUUID(4);
    }


    @PostMapping("/delAddress")
    @ApiOperation("根据地址id删除地址")
    public RequestResult<String> delAddress(@RequestParam String addrId) {

        if (null == addrId ) {
            return ResultBuildUtil.fail("503", "地址id不存在！");
        }
        // 构造条件
        UpdateWrapper<Address> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("addr_id", addrId);

        // 根据地址id删除地址
        if (addressService.remove(updateWrapper)) {
            log.info("地址删除成功{}", addrId);
            return ResultBuildUtil.success("删除成功！");
        }
        return ResultBuildUtil.fail("504", "地址删除失败！");
    }

    @PostMapping("/modAddress")
    @ApiOperation("修改地址")
    public RequestResult<Address> modAddress(@RequestBody Address address) {
        boolean update = addressService.updateAddr(address);
        if (update) {
            log.info("修改地址成功，修改地址id{}", address);
            return ResultBuildUtil.success(address);
        }

        return ResultBuildUtil.fail("505", "地址修改失败！");
    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/listAddress")
    @ApiOperation("查询指定用户的全部地址")
    public RequestResult<List<Address>> listAddress(@RequestParam String addrUserId) {

        // 条件构造器
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("addr_user_id", addrUserId);

        return ResultBuildUtil.success(addressService.list(queryWrapper));
    }


}
